using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using Microsoft.SharePoint;

namespace CKS.EBE
{
    public class XslMethods 
    {
        public static string buildQueryString(string newValues, string s1)
        {
            return buildQueryString(newValues, s1, "", "", "");
        }

        public static string buildQueryString(string newValues, string s1, string s2)
        {
            return buildQueryString(newValues, s1, s2, "", "");
        }

        public static string buildQueryString(string newValues, string s1, string s2, string s3)
        {
            return buildQueryString(newValues, s1, s2, s3, "");
        }

        public static string buildQueryString(string newValues, string s1, string s2, string s3, string s4)
        {
            StringBuilder oResult = new StringBuilder("?" + newValues);

            string[] remove = new string[] { s1, s2, s3, s4 };

            List<string> oKeysToRemove = new List<string>(remove);

            System.Collections.Specialized.NameValueCollection qs = HttpContext.Current.Request.QueryString;

            foreach (string sKey in qs.AllKeys)
            {
                if (!oKeysToRemove.Contains(sKey.ToLower()) && !oKeysToRemove.Contains(sKey))
                    oResult.Append(string.Format("&{0}={1}", sKey, qs[sKey]));
            }

            return oResult.ToString();
        }

        public static string createPostUrl(string postDate, string urlTitle, string postID)
        {
            return BlogContext.Current.UrlProvider.createPostUrl(postDate, urlTitle, postID);
        }

        public static string createPostUrl(string postDate, string urlTitle)
        {
            return BlogContext.Current.UrlProvider.createPostUrl(postDate, urlTitle, null);
        }

        public static string createAbsolutePostUrl(string postDate, string urlTitle)
        {
            return BlogContext.Current.UrlProvider.createAbsolutePostUrl(postDate, urlTitle, null);
        }

        public static string createTagUrl(string tag)
        {
            return BlogContext.Current.UrlProvider.createTagUrl(tag);
        }

        public static string createMonthUrl(string postDate)
        {
            return BlogContext.Current.UrlProvider.createMonthUrl(postDate);
        }

        public static string clientDateTime(string postDate)
        {
            try
            {
                DateTime dt = DateTime.Parse(postDate);
                return BlogContext.Current.ClientDateFromWebDate(dt).ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch
            {
            }

            return postDate;
        }

        public string res(string resKey)
        {
            return API.Localization.GetResource(resKey);
        }

        public string res(string resKey, string resFile)
        {
            return API.Localization.GetResource(resKey, resFile);
        }

        /// <summary>
        /// create the pdf export link only, if "non commercial" or a licence key was entered in the blog settings
        /// </summary>
        /// <param name="imageUrl">themes/Default/images/exportPDF.gif</param>
        /// <param name="postID">1</param>
        /// <returns><a href="?ID={ID}&amp;Export=PDF" target="_blank"><img src="{$RelativeUrl}/themes/Default/images/exportPDF.gif" /></a></returns>
        public string ExportPDF(string imageUrl, int postID)
        {
            var key = BlogContext.Current.Settings.PDFExportLicenseKey;
            if (key == "non commercial" || (!string.IsNullOrEmpty(key) && key.Length > 14))
            {
                var pdfIconUrl = string.Format("{0}/{1}", SPContext.Current.Web.ServerRelativeUrl, imageUrl).Replace("//", "/");
                string url = string.Format("<a href=\"?ID={0}&amp;Export=PDF\" target=\"_blank\"><img src=\"{1}\" border=\"0\" ></a>", postID, pdfIconUrl);
                return url;
            }
            return string.Empty;
        }

        public static string MakeTiny(string postDate, string urlTitle)
        {
            string urlToShorten = createAbsolutePostUrl(postDate, urlTitle);
            try
            {
                //Make a call to TinyUrl.com passing in the Url to shorten
                WebRequest request = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + urlToShorten);
                WebResponse response = request.GetResponse();
                string tinyUrl;
                using (StreamReader responseFromMakeTiny = new StreamReader(response.GetResponseStream()))
                {
                    //Read in the tiny Url
                    tinyUrl = responseFromMakeTiny.ReadToEnd();
                }
                return tinyUrl;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
                //If for some reason we get an error, return the original Url
                return urlToShorten;
            }
        }
    }
}
